<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <keep-alive>
      <component :is="type"></component>
    </keep-alive>
    <ul>
      <li @click="changeTitle('home')">首页</li>
      <li @click="changeTitle('kind')">分类</li>
      <li @click="changeTitle('cart')">购物车</li>
      <li @click="changeTitle('user')">我的</li>
    </ul>
  </div>
</body>
<script src="lib/vue.js"></script>
<script>
  // activated()：在vue对象存活的情况下，进入当前存在activated()函数的页面时，一进入页面就触发；可用于初始化页面数据、keepalive缓存组件后，可执行方法；
  // deactivated()：离开组件时执行；
  const Home = {
    template: `<div>首页<input type="text" placeholder="首页" /></div>`,
    mounted() { console.log('首页 mounted') },
    activated() { console.log('home  activated') },
    deactivated() { console.log('home  deactivated') },
  }
  const Kind = {
    template: `<div>分类<input type="text" placeholder="分类" /></div>`,
    mounted() { console.log('分类 mounted') },
    activated() { console.log('kind  activated') },
    deactivated() { console.log('kind  deactivated') },
  }
  const Cart = {
    template: `<div>购物车<input type="text" placeholder="购物车" /></div>`,
    mounted() { console.log('购物车 mounted()') },
    activated() { console.log('cart  activated') },
    deactivated() { console.log('cart  deactivated') },
  }
  const User = {
    template: `<div>我的<input type="text" placeholder="我的" /></div>`,
    mounted() { console.log('我的 mounted') },
    activated() { console.log('user  activated') },
    deactivated() { console.log('user  deactivated') },
  }

  new Vue({
    el: '#app',
    data: {
      type: 'home'
    },
    components: {
      Home: Home,
      Kind: Kind,
      Cart: Cart,
      User: User
    },
    methods: {
      changeTitle(type) {
        this.type = type
      }
    },
  })
</script>

</html>